package web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.CompraService;
import entity.Compra;
import entity.ItemCompra;

@Controller
@Scope("view")
public class RelatorioComprasMB extends BaseManagedBean implements Serializable {

	private static final long serialVersionUID = 2980992478110115218L;

	@Autowired
	private CompraService compraService;

	@NotNull
	private Date dataInicial;

	@NotNull
	private Date dataFinal;

	private List<Compra> listaCompras;

	private List<ItemCompra> listaItemCompra;

	private Compra compraSelecionada;
	
	private int totalItensComprado;

	private Log log = LogFactory.getLog(RelatorioComprasMB.class);

	public RelatorioComprasMB() {
		log.debug("Inicializou o RelatorioComprasMB.");
	}

	/**
	 * @see web.BaseManagedBean#postConstruct()
	 */
	@Override
	@PostConstruct
	public void postConstruct() throws Exception {
		super.postConstruct();
		iniciar();
	}

	private void iniciar() {
		listaCompras = new ArrayList<Compra>();

		dataInicial = new Date();
		dataFinal = new Date();

	}

	public void doBuscarCompraAction() {
		listaItemCompra = compraService.buscarCompraPorChave(
				compraSelecionada.getId()).getListaItemCompra();
		
		for (ItemCompra item : listaItemCompra){
			totalItensComprado += item.getQuantidade();
		}
	}

	public void doListarAction() {
		listaCompras = compraService.listarComprasPorPeriodo(dataInicial,
				dataFinal);
	}

	protected void limparTela() {
		iniciar();
	}

	public Date getDataInicial() {
		return dataInicial;
	}

	public void setDataInicial(Date dataInicial) {
		this.dataInicial = dataInicial;
	}

	public Date getDataFinal() {
		return dataFinal;
	}

	public void setDataFinal(Date dataFinal) {
		this.dataFinal = dataFinal;
	}

	public List<Compra> getListaCompras() {
		return listaCompras;
	}

	public Compra getCompraSelecionada() {
		return compraSelecionada;
	}

	public void setCompraSelecionada(Compra compraSelecionada) {
		this.compraSelecionada = compraSelecionada;
	}

	public List<ItemCompra> getListaItemCompra() {
		return listaItemCompra;
	}

	public void setListaItemCompra(List<ItemCompra> listaItemCompra) {
		this.listaItemCompra = listaItemCompra;
	}

	public int getTotalItensComprado() {
		return totalItensComprado;
	}

}
